package com.sibu.yx.promotion.common.util;

import com.doubo.common.exception.SystemException;
import com.doubo.common.model.response.CommonResponse;
import com.sibu.yx.promotion.common.enums.ErrorCodeEnum;

/**
 * Created by zhangcp on 2021-7-13.
 */
public class ResponseRender {

    public static <T> CommonResponse<T> render(T result) {
        CommonResponse<T> response = new CommonResponse<T>();
        response.setResult(result);
        if(response.isSuccess()){
            response.setError(ErrorCodeEnum.SUCCEED_CODE);
        }else{
            response.setError(ErrorCodeEnum.BEAN_BIZ_ERROR);
        }
        return response;
    }

    public static <T> CommonResponse<T> renderErr(String errorMsg) {
        CommonResponse<T> response = new CommonResponse<T>();
        response.setErrorCode(ErrorCodeEnum.ERR_UNKNOW_ERROR.getErrorCode());
        response.setErrorMsg(errorMsg);
        return response;
    }

    public static <T> CommonResponse<T> renderErr(SystemException ex) {
        CommonResponse<T> response = new CommonResponse<T>();
        response.setErrorCode(ex.getErrCode());
        response.setErrorMsg(ex.getErrReason());
        return response;
    }
}
